package com.cnyz.controller;

import com.cnyz.base.PageResponse;
import com.cnyz.base.ResponseResult;
import com.cnyz.dto.CourseFileDto;
import com.cnyz.service.CourseFileService;
import com.cnyz.service.TranscribeService;
import com.cnyz.service.impl.TrancribeServiceImp;
import com.cnyz.vo.CourseFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/course/file")
@RestController
public class CourseFileController {

    @Autowired
    private CourseFileService courseFileService;

    @Autowired
    private TranscribeService transcribeService;


    @PostMapping("/add")
    @ApiOperation("为课程添加文件")
    public ResponseResult addCourseFile(@ModelAttribute CourseFileDto courseFileDto) {
        courseFileService.addCourseFile(courseFileDto);
        return ResponseResult.success("课程文件上传成功");
    }

    @GetMapping("/page")
    @ApiOperation("分页查询课程对应的文件")
    public ResponseResult getCourseFilePage(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(required = false) Long course,
                               @RequestParam(required = false) String name) {

        PageResponse<CourseFileVo> pages=courseFileService.getCourseFilePage(pageNum, pageSize, course, name);
        return ResponseResult.success("查询成功", pages);
    }

    @DeleteMapping("/delete/id")
    @ApiOperation("删除课程文件")
    public ResponseResult deleteCourseFile(@RequestParam Long id) {
        courseFileService.deleteCourseFile(id);
        return ResponseResult.success("删除成功");
    }

    @PutMapping("/update")
    @ApiOperation("更新课程文件")
    public ResponseResult updateCourseFile(@ModelAttribute CourseFileDto courseFileDto) {
        courseFileService.updateCourseFile(courseFileDto);
        return ResponseResult.success("课程文件更新成功");
    }

    @PostMapping("/update/VideoText")
    @ApiOperation("课程文件转文字操作")
    public ResponseResult updateCourseFileText(@RequestParam Long id) {
        transcribeService.transcribeVideoToText(id);//课程转文字的操作
        return ResponseResult.success("课程文件转文字操作成功");
    }

    @GetMapping("/id")
    @ApiOperation("根据ID查询课程文件")
    public ResponseResult getCourseFile(@RequestParam Long id) {
        CourseFileVo courseFileVo = courseFileService.getCourseFileById(id);
        if (courseFileVo != null) {
            return ResponseResult.success("查询成功", courseFileVo);
        } else {
            return ResponseResult.error("课程文件不存在");
        }
    }
}
